
print("About to access_by_lua !")
--
--
--local transfer_request_query = ngx.req.get_uri_args()
--local res = ngx.location.capture("/authtest" , { args = transfer_request_query })
--ngx.header.content_type = "application/json;charset=utf8"
--local json = require "cjson";
--
--if res.status == ngx.HTTP_OK then
--    return
--end
--
--if res.status == 401 then
--    ngx.status = res.status
--
--    ngx.say(res.body)
--    ngx.exit(401)
--end
--
--if res.status == 403 then
--    ngx.say("haha 403")
--    ngx.exit(ngx.HTTP_FORBIDDEN)
--end
--
--if res.status == 503 then
--    ngx.exit(ngx.HTTP_METHOD_NOT_IMPLEMENTED)
--end
--
--ngx.exit(ngx.HTTP_INTERNAL_SERVER_ERROR)
----ngx.exit(507)

local arr = {"demo.lifecycle.pre1","demo.lifecycle.pre2"}

for i=1 , #arr do
    print("=======>"..tostring(i)..":"..arr[i]);
    local s = require(arr[i])
    s.filter();
end















local init_key = ngx.shared.lua_cache:get('init_key')
print('------ access_by_lua ------'..'init_key:'..init_key)


-- 限流

-- 鉴权


-- 获取缓存filter列表，依次执行


-- 根据扩展filter，请求api业务接口测试
local request_uri = ngx.var.request_uri
print('-------------------request_uri'..request_uri)

-- 请求userbyid()接口
-- 获取url中参数
local uri_args = ngx.req.get_uri_args()
if  uri_args["gateway"] ~=nil then
    print("gateway exsit"..uri_args["gateway"] );
    return ;
end
-- 获取参数名
local userId = uri_args["userId"]
-- 负载均衡业务服务主机地址
-- 。。
print("==========================begin http ");
local apiPath = 'http://127.0.0.1:9001/userbyid'
local paramsPath = '?gateway=1&userId='..userId

ngx.log(ngx.ERR, 'URL:', apiPath..paramsPath)

local httpc = require('resty.http').new()

-- request
local resp, err = httpc:request_uri(apiPath,{
    method = 'GET',
    path = paramsPath
})

print()

-- response
if not resp then
    ngx.log(ngx.ERR, 'request failed:', err)
    ngx.say('request failed:', err)
    return
end




    print("beging to print body " )
    ngx.req.read_body()
    print(ngx.req.get_body_data() )


--ngx.say('hello-----')
ngx.say(resp.body)

httpc:close()